@echo off
chcp 65001

echo === 云打印系统构建工具 ===

echo 1. 检查并安装必要的Python模块...
echo 1.1 检查cx_Freeze...
python -c "import cx_Freeze" >nul 2>&1
if errorlevel 1 (
  echo 未安装cx_Freeze模块，正在安装...
  pip install cx_Freeze
  if errorlevel 1 (
    echo 安装cx_Freeze失败
    pause
    exit /b 1
  )
)

echo 1.2 检查uvicorn...
python -c "import uvicorn.logging" >nul 2>&1
if errorlevel 1 (
  echo 安装uvicorn完整包...
  pip install uvicorn[standard]
  if errorlevel 1 (
    echo 安装uvicorn失败
    pause
    exit /b 1
  )
)

echo 1.3 检查fastapi...
python -c "import fastapi" >nul 2>&1
if errorlevel 1 (
  echo 安装fastapi...
  pip install fastapi
  if errorlevel 1 (
    echo 安装fastapi失败
    pause
    exit /b 1
  )
)

echo 1.4 检查jinja2...
python -c "import jinja2" >nul 2>&1
if errorlevel 1 (
  echo 安装jinja2...
  pip install jinja2
  if errorlevel 1 (
    echo 安装jinja2失败
    pause
    exit /b 1
  )
)

echo 1.5 安装其他可能缺少的依赖...
pip install pywin32 requests python-multipart
if errorlevel 1 (
  echo 警告: 某些依赖可能未正确安装，但将继续尝试构建
)

echo 2. 尝试结束相关进程...
taskkill /f /im cloud_print_service.exe >nul 2>&1
taskkill /f /im cloud_print_app.exe >nul 2>&1
timeout /t 2 >nul

echo 3. 开始构建...
python setup.py build
if errorlevel 1 (
  echo 错误: 构建失败
  pause
  exit /b 1
)

echo 4. 创建必要目录...
md build\exe\logs 2>nul
md build\exe\temp 2>nul
md build\exe\queue 2>nul
md build\exe\watch_folder 2>nul
md build\exe\print_status 2>nul

echo 5. 复制必要文件...
copy install_service.bat build\exe\ >nul
copy uninstall_service.bat build\exe\ >nul
copy create_shortcut.bat build\exe\ >nul
copy create_shortcut.py build\exe\ >nul
copy config.ini build\exe\ >nul

echo === 构建完成 ===
echo 请进入build\exe目录，运行cloud_print_app.exe或者install_service.bat
pause 